﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using orz.mouxianyu.Services;
using orz.mouxianyu.Extensions.ToolClass;
using Microsoft.AspNetCore.Http;

namespace orz.mouxianyu.Controllers
{
    /// <summary>
    /// 测试controller
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]

    public class TestController : ControllerBase
    {
        private ITestService _ITestService;
        private IHttpContextAccessor _accessor;

        public TestController(ITestService TestService, IHttpContextAccessor accessor)
        {
            this._ITestService = TestService;//注入service业务层
            _accessor = accessor;//获取请求信息
        }

        /// <summary>
        /// 测试接口1
        /// </summary>
        /// <param name="number">请输入一个数字</param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        [Route("Test1")]

        public IActionResult test1(int number)
        {
            var re = _ITestService.test(number);
            var ar = new ApiOkResponse(re);
            return Ok(ar);
        }

        /// <summary>
        /// 数据库连接测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        [Route("TestSql")]

        public IActionResult TestSql()
        {
            var re = _ITestService.TestSql();
            var ar = new ApiOkResponse(re);
            return Ok(ar);
        }

        /// <summary>
        /// 身份验证测试测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("TestAuth0")]

        public IActionResult TestAuth0()
        {
            string role = User.Claims.Where(P => P.Type == "rol").SingleOrDefault().Value;
            if (role == "admins")
            {
                var re = new
                {
                    remsg = "身份验证通过"
                };
                var ar = new ApiOkResponse(re);
                return Ok(ar);
            }
            else
            {
                return StatusCode(403, "非管理员用户");
            }
        }

        /// <summary>
        /// ip与浏览器信息获取测试
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        [Route("getAccessor")]

        public IActionResult getAccessor()
        {
            return Ok(_accessor.HttpContext.Connection.RemoteIpAddress.ToString());
        }
    }
}
